﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinImageTag
{
    public partial class FrmSearch : Form
    {
        public FrmSearch()
        {
            InitializeComponent();
        }

        private void FrmSearch_Load(object sender, EventArgs e)
        {
            //读取标签
            string[] tags = ConfigUtils.tags.Split(' ');
            foreach (string tag in tags)
            {
                chklstTags.Items.Add(tag);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lvImages.Clear();
            //读取目录下的所有文件
            string[] files = Directory.GetFiles(ConfigUtils.imgPath, "*");
            foreach (string file in files)
            {
                if (IsFind(file))
                {
                    //展示到列表
                    lvImages.Items.Add(file);
                }
            }
        }

        /// <summary>
        /// 判断一个文件名是否包含在选中的标签里
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool IsFind(String fileName)
        {
            foreach(string tag in chklstTags.CheckedItems)
            {
                if (fileName.Contains(tag))
                {
                    return true;
                }
            }
            return false;
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if(dr == DialogResult.OK)
            {
                foreach(ListViewItem item in lvImages.Items)
                {
                    File.Copy(item.Text, folderBrowserDialog1.SelectedPath+"\\"+ Path.GetFileName(item.Text));
                }
                MessageBox.Show("保存成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
